﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LSQL;

namespace Shopfloormanagement
{
    public partial class Keycalendar_input : Form
    {
        public static Form fr = new Keycalendar_input();
        public static Keycalendar_input instance = new Keycalendar_input();
        public Keycalendar_input()
        {
            instance = this;
            InitializeComponent();
        }
        
        public string EventDate = DateTime.Now.ToString("d");
        public string EventTime = DateTime.Now.TimeOfDay.ToString();
        public string EventText = string.Empty;
        public string EventID = string.Empty;

        private void Keycalendar_input_Load(object sender, EventArgs e)
        {
            fr = this;
            //Form_display_Load(sender, e);
            dtp_Event.Value = DateTime.Parse( EventDate);
            tp_Event.Value = DateTime.Parse( EventTime);
            txt_Event.Text = EventText;
            lab_tip.Text = EventID;

            tp_Event.Text = "08:00";
        }

        private bool saved = false;

        private void Write2SQL()

        {
            string text = txt_Event.Text;
            string due_date = dtp_Event.Value.ToString("yyyy/MM/dd");
            string Time = tp_Event.Text;
            int user = SystemInfo.userID;

            if (text == "" || text == null)
            {
                return;
            }
            if (due_date == "" || due_date == null)
            {
                return;
            }
            if (Time == "" || Time == null)
            {
                return;
            }
            
            string columns = "Key_calendar,Due_date,Time,Create_userID";
            string value = $"'{text}','{due_date}','{Time}',{user}";
            int result = SQL.InsertReturnID(SystemInfo.SQL, "Key_calendar", columns, value);
           
            if (result > 0)
            {
                MessageBox.Show("成功保存", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                saved = true;
            }
            else
            {
                MessageBox.Show("保存失败", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                saved = false;
            }
        }

        private void Update2SQL()

        {
            string text = txt_Event.Text;
            string due_date = dtp_Event.Value.ToString("yyyy/MM/dd");
            string Time = tp_Event.Text;
            int user = SystemInfo.userID;

            if (text == "" || text == null)
            {
                return;
            }
            if (due_date == "" || due_date == null)
            {
                return;
            }
            if (Time == "" || Time == null)
            {
                return;
            }

            bool update = SQL.Update(SystemInfo.SQL, "Key_calendar", $"Key_calendar = '{text}',Due_date = '{due_date}',Time = '{Time}',Create_userID = {user}", $"ID = {EventID}");

            if (update)
            {
                MessageBox.Show("成功保存!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                saved = true;
            }
            else
            {
                MessageBox.Show("保存失败", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                saved = false;
            }
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            if (EventID == string.Empty || EventID == "")
            {
                //说明是新信息
                Write2SQL();
            }
            else
            {
                Update2SQL();
            }
            
            this.Close();
        }

        
        
    }
}